package com.designCreator.model.business
{
	import mx.rpc.AsyncToken;
	import mx.rpc.IResponder;
	import mx.rpc.Responder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
	public class LoadFeedDelegate
	{
		private var _service : HTTPService;
		
		private var _token : AsyncToken;
		
		public function LoadFeedDelegate()
		{
			_service = new HTTPService();
			_service.resultFormat = "e4x";
		}
		
		public function send( url : String ) : AsyncToken
		{
			_service.url = url;
			var localResponder : Responder = new Responder( feedLoaded, loadFailed );
			var token : AsyncToken = _service.send();
			token.addResponder( localResponder );
			_token = new AsyncToken( token.message );
			return _token;
		}
		
		/*private function feedLoaded( event : ResultEvent ) : void
		{
			var itunes : Namespace = new Namespace( "http://www.itunes.com/dtds/podcast-1.0.dtd" );
			var feed : Feed = new Feed;
			var channel : XML = ( event.result as XML ).channel[ 0 ];
			feed.title = channel.title;
			feed.url = channel.link;
			feed.description = channel.itunes::summary;
			if ( !feed.description )
			{
				feed.description = channel.description;
			}
			feed.image = channel.itunes::image.@href;
			
			feed.podcasts = new Array();
			for each ( var item : XML in channel.item )
			{
				var podcast : Podcast = new Podcast();
				podcast.title = item.title;
				podcast.description = item.itunes::summary;
				if ( !podcast.description )
				{
					podcast.description = item.description;
				}
				podcast.url = item.enclosure.@url;
				podcast.format = item.enclosure.@type;
				podcast.length = item.enclosure.@length;
				podcast.pubDate = new Date( Date.parse( item.pubDate.toString() ) );
				feed.podcasts.push( podcast );
			}
			
			feed.loaded = true;
			var resultEvent : ResultEvent = new ResultEvent( ResultEvent.RESULT, false, true, feed, _token );
			for each ( var responder : IResponder in _token.responders )
			{
				responder.result( resultEvent );
			}
		}*/
		
		private function loadFailed( event : FaultEvent ) : void
		{
			for each ( var responder : IResponder in _token.responders )
			{
				responder.fault( event );
			}
		}
	}
}